package com.atguigui.leetcode1;

import java.util.HashMap;
import java.util.Map;

/**
 * 2404.出现最频繁的偶数元素
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/4/13 13:26
 */
public class P2404MostFrequentEvenElement {
    public static void main(String[] args) {
        Solution solution = new P2404MostFrequentEvenElement().new Solution();
        // TO TEST
    }

    class Solution {
        public int mostFrequentEven(int[] nums) {

            HashMap<Integer, Integer> map = new HashMap<>();
            for (int num : nums) {
                if (num % 2 == 0) {
                    map.put(num, map.getOrDefault(num, 0) + 1);
                }
            }

            int res = -1, ct = 0;
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if (res == -1 || entry.getValue() > ct || entry.getValue() == ct && res > entry.getKey()) {
                    res = entry.getKey();
                    ct = entry.getValue();
                }
            }

            return res;

        }
    }
}
